package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Certificate;
import com.blb.entity.User;
import com.blb.service.ICertificateService;
import com.blb.service.IUserService;
import com.blb.util.ResponseHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PangLei
 * @since 2019-12-23
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICertificateService certificateService;

    /*通过id得到用户阅读密码*/
    @GetMapping("getReadPassword")
    public String getReadPassword(@RequestParam String id){
        return userService.getById(id).getReadPassword();
    }

    /**
     * 根据id改变省市区
     * @param id
     * @param province
     * @param city
     * @param region
     */
    @PostMapping("updateaddress")
    public void updateaddress(String id ,String province,String city,String region){
        userService.updateaddress(id, province, city, region);
    }

    /**
     * 修改昵称
     * @param id
     * @param nickname
     */
    @PostMapping("updateNickname")
    public void updateNickname(String id ,String nickname){
        userService.updateNickname(id,nickname);
    }

    /**
     * 修改性别
     * @param id
     * @param sex
     */
    @PostMapping("updateUserSex")
    public void updateSex(String id ,String sex){
        userService.updateSex(id, sex);
    }

    /**
     * 修改简介
     * @param id
     * @param signature
     */
    @PostMapping("updateUserSignature")
    public void updateSignature(String id ,String signature){
        userService.updateSignature(id, signature);
    }


    /**
     * 修改邮箱
     * @param id
     * @param email
     */
    @PostMapping("updateEmail")
    public String updateEmail(String id ,String passwordxx, String email){
        String msg ;

        Certificate certificate = certificateService.findPasswordById(id);

        if (certificate.getToken().equals(passwordxx)){
            //修改个人信息的邮箱
            userService.updateEmail(id, email);
            //修改凭证表里的邮箱
            certificateService.updateCertificate(id, email);
            msg ="修改邮箱成功";
            return msg;
        }else {
            msg= "密码错误，请正确输入密码";
            return msg;
        }

    }



     /**
     * 根据id得到user信息
     * @param id
     * @return
     */
    @PostMapping("getUser")
    public User getUser(String id){
       return userService.getById(id);
    }

    /*上传头像*/
    @PostMapping("upload")
    public ResponseHelp upload(MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\whale_notes\\note_web\\src\\main\\resources\\static\\img\\head_img\\" + name));
        uploadImg(name);
        return ResponseHelp.success(name);
    }

    /*复制头像*/
    public void uploadImg(String name) throws IOException {
        //创建一个FileOutputStream对象(目标路径)
        FileOutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\whale_notes\\note_web\\target\\classes\\static\\img\\head_img\\" + name);
        //创建一个FileInputStream对象(源文件路径)
        FileInputStream in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\whale_notes\\note_web\\src\\main\\resources\\static\\img\\head_img\\" + name);
        //进行复制操作
        int len = 0;
        byte[] byts = new byte[1024];
        while ((len = in.read(byts)) != -1) {
            //注意：字节流会直接写入到文件当中，不用刷新缓存
            out.write(byts, 0, len);
        }
        //释放资源
        in.close();
        out.close();
    }

    /*更新用户*/
    @PostMapping("update")
    public ResponseHelp update(User user){
        userService.updateById(user);
        return ResponseHelp.success("修改成功");
    }

    @PostMapping("setReadPwd")
    public ResponseHelp setReadPwd(String id,String password1,String password2){
        if (!password1.equals(password2)) {
            return ResponseHelp.error("两次输入密码不一致，请重新输入");
        }
        User user = new User();
        user.setId(id);
        user.setReadPassword(password1);
        userService.updateById(user);
        return ResponseHelp.success("设置成功");
    }

    @PostMapping("updateReadPwd")
    public ResponseHelp updateReadPwd(String id,String passwordxx,String password1,String password2){
        System.out.println("updateReadPwd");
        System.out.println(passwordxx);
        User user = userService.getById(id);
        System.out.println(user);
        if (!user.getReadPassword().equals(passwordxx)) {
            return ResponseHelp.error("原阅读密码错误，请重新输入");
        }
        if (!password1.equals(password2)) {
            return ResponseHelp.error("两次输入新密码不一致，请重新输入");
        }
        user.setReadPassword(password1);
        userService.updateById(user);
        return ResponseHelp.success("修改成功");
    }
    /**
     * 查找分享笔记用户信息
     * @param userId
     * xuyuanxin
     * @return
     */
    @PostMapping("getShareUser")
    public User getShareUser(String userId){
        User user = userService.getOne(new QueryWrapper<User>().eq("id", userId));
        return user;
    }

}

